(function(){
    angular.module("loginCtrlModule",[])
        .controller("loginController",["$scope",
            "postDataService","messageService",
            "modalDataService","USERDATA","$rootScope",
            "$ionicModal",
            function($scope,postDataService,
                     messageService,
                     modalDataService,
                     USERDATA,
                     $rootScope,
                     $ionicModal){
                $scope.user = {};
                $scope.loginClick = function(){
                    messageService.showLoading("正在登录中...");
                    postDataService.postRequest(
                        "userInfoLogin.php",
                        {
                           username:$scope.user.username,
                           password:hex_md5($scope.user.password)
                        },
                        function(data){
                            messageService.hideLoading();
                            console.log(data);
                            if(data.code==0){
                                // 关闭模态框
                                modalDataService.myModal.remove();
                                // 数据存储 存储到单例  各个控制器都可以使用
                                // 下单是需要用户的id信息
                                USERDATA.user = data.data;
                                USERDATA.isLogin = false;
                                $rootScope.$broadcast("loginSuccess");
                                $rootScope.$emit("loginSuccess");
                            }else {
                                messageService.showMessage(data.data);
                            }
                        },
                        function(error){
                            messageService.hideLoading();
                            console.log(error);
                            messageService.showMessage(error);
                        }
                    )


                };

                $scope.FanHui = function(){
                    console.log(11);
                    modalDataService.myModal.remove();
                }

                $scope.zhuceClick = function(){
                    modalDataService.myModal.hide();
                    $ionicModal.fromTemplateUrl('tpl/zhuce.html', {
                        animation: 'slide-in-up'
                    }).then(function(modal) {
                        $scope.modal = modal;
                        modalDataService.myModal = modal;
                        $scope.modal.show();
                        $rootScope.$broadcast("passedRegistModal",$scope.modal);
                    });
                };
        }])
})();